//
//  LocationSelectVC.m
//  zjGovernment
//
//  Created by 胡胜冬 on 2017/2/18.
//  Copyright © 2017年 胡胜冬. All rights reserved.
//

#import "LocationSelectVC.h"
#import "LeftCityView.h"
#import "RightAreaView.h"
#import "DDbaseNetWork.h"
@interface LocationSelectVC ()<LeftCityViewDelegate, RightAreaViewDelegate>

@property(nonatomic, strong) LeftCityView *leftView;
@property(nonatomic, strong) RightAreaView *rightView;

@property(nonatomic, strong) NSString *leftSelectString;
@property(nonatomic, strong) NSString *leftSelectID;
@property(nonatomic, strong) NSString *leftSelectCode;

@property(nonatomic, strong) NSString *rightSelectString;
@property(nonatomic, strong) NSString *rightSelectID;
@property(nonatomic, strong) NSString *rightSelectCode;

@end

@implementation LocationSelectVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavi];
    [self initLeftView];
    [self initRightView];
    [self getCityDatas];
}

- (void)initNavi {
    self.title = NSLocalString(@"站点列表");
}

- (void)initLeftView {
    self.leftView = [[LeftCityView alloc] initWithFrame:CGRectMake(0, 0, 100 * APP_PARAMETER, self.view.bounds.size.height - 64.0)];
    self.leftView.delegate = self;
    [self.view addSubview:self.leftView];
}

- (void)initRightView {
    self.rightView = [[RightAreaView alloc] initWithFrame:CGRectMake(100 * APP_PARAMETER, 0, self.view.bounds.size.width - 100 * APP_PARAMETER, self.view.bounds.size.height - 64.0)];
    self.rightView.delegate = self;
    [self.view addSubview:self.rightView];
}

#pragma mark - 网络请求
- (void)getCityDatas {
    WEAKSELF
    NSDictionary *params = @{
                             @"parentCode":@"410000"
                             };
    DDnetWrokEngine *engine = [DDnetWrokEngine engineWithControl:self BaseUrl:KUSERUPDATEBASEURL requestUrl:@"/city/listChildren?=410000" requestType:DDrequestTypeGET requestParams:params success:^(id result, DDcacheType cacheType, DDresultType resultType) {
        if (result[@"data"]) {
            NSDictionary *provinceDic = result[@"data"];
            NSArray *cityArray = provinceDic[@"children"];
            if (weakSelf.leftView) {
                weakSelf.leftView.cityDataArray = cityArray;
            }
        }
    } failure:^(NSError *error) {
    }];
    engine.cacheTime = 60 * 60 * 24 * 30;
    [DDNetWorkManager callRequestWithWorkEngnine:engine];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - LeftCityViewDelegate
- (void)clickLeftCityViewCellWithArray:(NSArray *)dataArray Title:(NSString *)titleString ID:(NSString *)idString Code:(NSString *)codeString{
    
    self.leftSelectString = titleString;
    self.leftSelectID = idString;
    self.leftSelectCode = codeString;
    
    self.rightView.areaArray = dataArray;
}

#pragma mark - RightAreaViewDelegate
- (void)clickRightAreaViewCellWithTitle:(NSString *)titleString ID:(NSString *)idString Code:(NSString *)codeString{
    
    self.rightSelectString = titleString;
    self.rightSelectID = idString;
    self.rightSelectCode = codeString;
    
    NSString *showString = [NSString stringWithFormat:@"%@,%@",self.leftSelectString, self.rightSelectString];
    if ([self.delegate respondsToSelector:@selector(selectLocationNameWithString:)]) {
        [self.navigationController popViewControllerAnimated:YES];
        [self.delegate selectLocationNameWithString:showString];
    }
}

@end
